<?php
/**
 * Remove an experience from a contact
 * 
 * @author	Xavier VILAIN
 * @package Contact\Experience
 * @license #hive-contact#
 */
use Fox\Behaviors;
use Fox\Generator;
use Fox\Popup\ToConfirm\Deletion;
$xview = Xview::instance();

$popup = new Deletion('pDelete',
			Hive\Ressource\Experience::className(),			
			Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_DELETE_HDR),
			array(Fox\Msg::BUNDLE,Fox\Msg::MSG_DELETE_MSG_CONF),
			array(Fox\Msg::BUNDLE,Fox\Msg::MSG_DELETE)
			,Generator::ajaxReload('contentCol')
			);
$popup->deletionGenerator(Generator::useFunction(function($experience) {
	Hive\Ressource::className();//Build the link
	$current = $experience->current;
	$expid = $experience->expid;
	$contact = $experience->ressource();
	$contact->deleteFromExperiences($experience);
	$start_date = null;
	foreach($contact->experiences() as $iExperience) {
		if($iExperience->start_date > $start_date && $iExperience->expid != $expid) {
			$start_date = $iExperience->start_date;
			$currentExperience = $iExperience;
		}
	}
	if(!empty($currentExperience)) {
		$currentExperience->current = 1;
		$contact->replaceExperiences($currentExperience);
	}
	$contact->prepareUpdate(\Hive\Ressource::CHANGE_EXPERIENCE_DELETE);
	$contact->update();
}));
echo $popup->render();